home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / misc / math / Matrix.lha / Matrix.c < prev    next >
C/C++ Source or Header  |  1998-04-29  |  4KB  |  140 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. //#include <conio.h>
  4. #define clrscr() printf("\f");
  5.  
  6. int main(void)
  7. {
  8.    int x, y, z, xx, yy, rows;
  9.    float matrix[10][11] = {0}, answer[10][2] = {0}, temp1, temp2;
  10.  
  11.    do
  12.    {
  13.       clrscr();
  14.       printf("Enter the number of rows... (May not exceed 9)");
  15.       scanf("%d", &rows);
  16.    } while ((rows<0) || (rows>9));
  17.    printf("Enter %d numbers to make a matrix.\n", rows*(rows+1));
  18.    printf("The first group of numbers keyed in will be the first row.\n");
  19.    printf("The second group of numbers will be the second row, etc...\n");
  20.    printf("The numbers must be separated by hitting <ENTER>.\n");
  21.    for (y = 1; y<=rows; y++)
  22.    {
  23.       for (x = 1; x<=rows+1; x++)
  24.       {
  25.          scanf("%f", &matrix[y][x]);
  26.       }
  27.    }
  28.    clrscr();
  29.  
  30.    for (y = 1; y<=rows; y++)
  31.    {
  32.       for (x = 1; x<=rows+1; x++)
  33.       {
  34.          printf("%10.0f", matrix[y][x]);
  35.       }
  36.       printf("\n");
  37.    }
  38.    printf("\nHit <ENTER> to continue...\n");
  39.    while (getch()!=13) ;
  40.  
  41.    for (z = 1; z<=rows-1; z++)
  42.    {
  43.       for (y = rows-1; y>=z; y--)
  44.       {
  45.          if (matrix[y+1][z])
  46.          {
  47.             temp2 = matrix[y+1][z];
  48.             if (matrix[y][z])
  49.             {
  50.                temp1 = matrix[y][z];
  51.                for (x = 1; x<=rows+1; x++)
  52.                {
  53.                   matrix[y][x] = matrix[y][x] * temp2;
  54.                   matrix[y+1][x] = matrix[y+1][x] * temp1;
  55.                   matrix[y+1][x] = matrix[y][x] - matrix[y+1][x];
  56.                   matrix[y][x] = matrix[y][x] / temp2;
  57.                }
  58.             }
  59.             else
  60.             {
  61.                if (matrix[1][z]==0) for (x = 1; x<=rows+1; x++)
  62.                   matrix[1][x] = matrix[y+1][x] - matrix[1][x];
  63.                temp1 = matrix[1][z];
  64.                for (x = 1; x<=rows+1; x++)
  65.                {
  66.                   matrix[1][x] = matrix[1][x] * temp2;
  67.                   matrix[y+1][x] = matrix[y+1][x] * temp1;
  68.                   matrix[y+1][x] = matrix[1][x] - matrix[y+1][x];
  69.                   matrix[1][x] = matrix[1][x] / temp2;
  70.                }
  71.             }
  72.             for (x = 1; x<=rows && matrix[y+1][x]==0; x++) ;
  73.             if (x > rows)
  74.             {
  75.                if (matrix[y+1][rows+1]) printf("\nNo solution set.\n");
  76.                else printf("\nInfinite solution set.\n");
  77.  
  78.                printf("\nHit <Esc> to exit...\n");
  79.                while (getch()!=27) ;
  80.                exit(0);
  81.             }
  82.  
  83.             clrscr();
  84.             for (yy = 1; yy<=rows; yy++)
  85.             {
  86.                for (xx = 1; xx<=rows+1; xx++)
  87.                {
  88.                   printf("%10.0f", matrix[yy][xx]);
  89.                }
  90.                printf("\n");
  91.             }
  92.             printf("\nHit <ENTER> to continue...\n");
  93.             while (getch()!=13) ;
  94.          }
  95.       }
  96.    }
  97.  
  98.    for (y = rows; y>=1; y--)
  99.    {
  100.       for (x = rows; x>=y; x--)
  101.       {
  102.          if (answer[x][1])
  103.          {
  104.             matrix[y][x] = matrix[y][x] * answer[x][0];
  105.             matrix[y][rows+1] = matrix[y][rows+1]-matrix[y][x];
  106.             matrix[y][x] = 0;
  107.          }
  108.          else
  109.          {
  110.             matrix[y][x] = matrix[y][rows+1]/matrix[y][x];
  111.             answer[x][0] = matrix[y][x];
  112.             answer[x][1] = -1;
  113.          }
  114.  
  115.          clrscr();
  116.          for (yy = 1; yy<=rows; yy++)
  117.          {
  118.             for (xx = 1; xx<=rows+1; xx++)
  119.             {
  120.                printf("%10.0f", matrix[yy][xx]);
  121.             }
  122.             printf("\n");
  123.          }
  124.          printf("\nHit <ENTER> to continue...\n");
  125.          while (getch()!=13) ;
  126.       }
  127.    }
  128.  
  129.    printf("\n");
  130.    for (x = 1; x<rows+1; x++)
  131.    {
  132.       printf("x^%d = %f\n", x, answer[x][0]);
  133.    }
  134.  
  135.    printf("\nHit <Esc> to exit...\n");
  136.    while (getch()!=27) ;
  137.    exit(0);
  138. }
  139.  
  140.